package com.sanhuo.idms.service;

import com.sanhuo.idms.bo.CollegeSaveBO;
import com.sanhuo.idms.dao.ClasMapper;
import com.sanhuo.idms.dao.CollegeMapper;
import com.sanhuo.idms.entity.College;
import com.sanhuo.lib.ExceptionUtil;
import com.sanhuo.security.UserVo;
import com.sanhuo.triild.Basic.BaseMapper;
import com.sanhuo.triild.Basic.BaseService;
import com.sanhuo.triild.annotation.MapperWire;
import com.sanhuo.utils.basic.BeanUtil;
import com.sanhuo.utils.basic.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CollegeService extends BaseService<College> {
    @MapperWire
    private CollegeMapper collegeMapper;
    @MapperWire
    private ClasMapper clasMapper;


    @Override
    public BaseMapper<College> getBaseMapper() {
        return collegeMapper;
    }

    public void remove(String collegeId) {
        ExceptionUtil.throwException(clasMapper.countByCollegeId(collegeId) > 0, "该学院存在班级,不可删除!");
        delete(collegeId);
    }

    public void save(final UserVo user, CollegeSaveBO bo) throws IllegalAccessException {
        if (StringUtil.isNotBlank(bo.getId())) {
            List<String> ids = collegeMapper.findIds();
            for (String id : ids) {
                ExceptionUtil.throwException(id.equals(bo.getId()), "该学院序号已被占用");
                break;
            }
        }
        College college = new College();
        BeanUtil.copyBean(bo, college);
        college.creatorAndUpdatorInfo(user);
        save(college);
    }

    public void delete(String id) {
        collegeMapper.remove(id);
    }

}
